package com.pilot.game.service;

import net.glxn.qrgen.QRCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

/**
 * QrCodeService
 *
 * @author yhq
 * @version 2017/12/16 下午10:10
 */

@Service
public class QrCodeService {

    private final Logger log = LoggerFactory.getLogger(UserService.class);

    private final FileUploadService fileUploadService;

    public QrCodeService(FileUploadService fileUploadService) {
        this.fileUploadService = fileUploadService;
    }

    /**
     * 二维码生成
     *
     * @param content 二维码内容
     * @return 二维码地址
     */
    public String generateQrCode(String content) {
        if (StringUtils.isBlank(content)) {
            return StringUtils.EMPTY;
        }
        String fileName = DateFormatUtils.format(Date.from(Instant.now()), "MM-dd-HH-mm");
        return fileUploadService.uploadFile(QRCode.from(content).stream().toByteArray(), fileName);
    }
}
